package com.redteam.xomtro.fragment;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.redteam.xomtro.R;
import com.redteam.xomtro.classes.Support;
import com.slidingmenu.lib.SlidingMenu;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressWarnings("ConstantConditions")
public class MainMenuFragment extends Fragment implements View.OnClickListener {

    private SlidingMenu slidingMenu;
    private TextView actionBarTitle;
    View selectedItem;
    private View menuAllContent;
    private View menuTrafficContent;
    private View menuDateContent;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View r = inflater.inflate(R.layout.menu_left, container, false);
        assert r != null;
        //r.findViewById(R.id.btnMenuAdd).setOnClickListener(createEvent);
        selectedItem = r.findViewById(R.id.btnMenuHome);
        int[] menuItemId = new int[]{R.id.btnMenuHome, R.id.btnMenuHot, R.id.btnMenuStar,
                R.id.btnMenuFriend, R.id.btnMenuPlace, R.id.btnMenuAll_music,
                R.id.btnMenuAll_movie, R.id.btnMenuAll_entertainment, R.id.btnMenuAll_fair,
                R.id.btnMenuAll_conference, R.id.btnMenuAll_sport, R.id.btnMenuAll_hotdeal,
                R.id.btnMenuAll_contest, R.id.btnMenuAll_education, R.id.btnMenuAll_other,
                R.id.btnMenuTraffic_jam, R.id.btnMenuTraffic_accident, R.id.btnMenuTraffic_mudslide,
                R.id.btnMenuTraffic_flooding, R.id.btnMenuTraffic_fire, R.id.btnMenuTraffic_other,
                R.id.btnMenuDate_today, R.id.btnMenuDate_tomorrow, R.id.btnMenuDate_weekend
        };
        for (int i : menuItemId) {
            r.findViewById(i).setOnClickListener(this);
        }

        // Categories group
        r.findViewById(R.id.btnMenuAll).setOnClickListener(groupClicked);
        menuAllContent = r.findViewById(R.id.btnMenuAll_content);

        r.findViewById(R.id.btnMenuTraffic).setOnClickListener(groupClicked);
        menuTrafficContent = r.findViewById(R.id.btnMenuTraffic_content);
        r.findViewById(R.id.btnMenuDate).setOnClickListener(groupClicked);
        menuDateContent = r.findViewById(R.id.btnMenuDate_content);
        return r;
    }

//    View.OnClickListener createEvent = new View.OnClickListener() {
//        @Override
//        public void onClick(View view) {
//            startActivity(new Intent(getActivity(), CreateEventActivity.class));
//        }
//    };

    View.OnClickListener groupClicked = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageView icon = (ImageView) view.findViewWithTag("grouparrow");
            if (view.getTag().toString().equals("collapsed")) {
                icon.setImageResource(R.drawable.ic_menu_arrow_up);
                switch (view.getId()) {
                    case R.id.btnMenuAll:
                        expand(menuAllContent);
                        break;
                    case R.id.btnMenuTraffic:
                        expand(menuTrafficContent);
                        break;
                    case R.id.btnMenuDate:
                        expand(menuDateContent);
                        break;
                }
                view.setTag("");
            } else {
                icon.setImageResource(R.drawable.ic_menu_arrow_down);
                switch (view.getId()) {
                    case R.id.btnMenuAll:
                        collapse(menuAllContent);
                        break;
                    case R.id.btnMenuTraffic:
                        collapse(menuTrafficContent);
                        break;
                    case R.id.btnMenuDate:
                        collapse(menuDateContent);
                        break;
                }
                view.setTag("collapsed");
            }
        }
    };

    @Override
    public void onClick(View view) {
        if (selectedItem == view) {
            slidingMenu.showContent(true);
            return;
        }
        String selectedItemTag = "";
        String viewTag = "";
        if (selectedItem.getTag() != null) selectedItemTag = selectedItem.getTag().toString();
        if (view.getTag() != null) viewTag = view.getTag().toString();
        if (!selectedItemTag.equals(viewTag)) {
            if (selectedItemTag.equals("publicChild"))
                ((ViewGroup) getView().findViewById(R.id.btnMenuAll)).getChildAt(0).setVisibility(View.INVISIBLE);
            else if (selectedItemTag.equals("trafficChild"))
                ((ViewGroup) getView().findViewById(R.id.btnMenuTraffic)).getChildAt(0).setVisibility(View.INVISIBLE);
            else if (selectedItemTag.equals("dateChild"))
                ((ViewGroup) getView().findViewById(R.id.btnMenuDate)).getChildAt(0).setVisibility(View.INVISIBLE);
            if (viewTag.equals("publicChild"))
                ((ViewGroup) getView().findViewById(R.id.btnMenuAll)).getChildAt(0).setVisibility(View.VISIBLE);
            else if (viewTag.equals("trafficChild"))
                ((ViewGroup) getView().findViewById(R.id.btnMenuTraffic)).getChildAt(0).setVisibility(View.VISIBLE);
            else if (viewTag.equals("dateChild"))
                ((ViewGroup) getView().findViewById(R.id.btnMenuDate)).getChildAt(0).setVisibility(View.VISIBLE);
        }
        ((ViewGroup) selectedItem).getChildAt(0).setVisibility(View.INVISIBLE);
        ((ViewGroup) view).getChildAt(0).setVisibility(View.VISIBLE);
        selectedItem = view;
        actionBarTitle.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/segoeui_sb.ttf"));
        switch (view.getId()) {
            case R.id.btnMenuHot:
                actionBarTitle.setText(getString(R.string.txt_menu_hot_item));
                break;
            case R.id.btnMenuStar:
                actionBarTitle.setText(getString(R.string.txt_menu_star_item));
                break;
            case R.id.btnMenuFriend:
                actionBarTitle.setText(getString(R.string.txt_menu_friend_item));
                break;
            case R.id.btnMenuPlace:
                actionBarTitle.setText(getString(R.string.txt_menu_place_item));
                break;
            case R.id.btnMenuAll_music:
                actionBarTitle.setText(getString(R.string.txt_menu_music_item));
                break;
            case R.id.btnMenuAll_movie:
                actionBarTitle.setText(getString(R.string.txt_menu_movie_item));
                break;
            case R.id.btnMenuAll_entertainment:
                actionBarTitle.setText(getString(R.string.txt_menu_entertainment_item));
                break;
            case R.id.btnMenuAll_fair:
                actionBarTitle.setText(getString(R.string.txt_menu_fair_item));
                break;
            case R.id.btnMenuAll_conference:
                actionBarTitle.setText(getString(R.string.txt_menu_conference_item));
                break;
            case R.id.btnMenuAll_sport:
                actionBarTitle.setText(getString(R.string.txt_menu_sport_item));
                break;
            case R.id.btnMenuAll_hotdeal:
                actionBarTitle.setText(getString(R.string.txt_menu_hotdeal_item));
                break;
            case R.id.btnMenuAll_contest:
                actionBarTitle.setText(getString(R.string.txt_menu_contest_item));
                break;
            case R.id.btnMenuAll_education:
                actionBarTitle.setText(getString(R.string.txt_menu_education_item));
                break;
            case R.id.btnMenuAll_other:
                actionBarTitle.setText(getString(R.string.txt_menu_public_group));
                break;
            case R.id.btnMenuTraffic_jam:
                actionBarTitle.setText(getString(R.string.txt_menu_jam_item));
                break;
            case R.id.btnMenuTraffic_accident:
                actionBarTitle.setText(getString(R.string.txt_menu_accident_item));
                break;
            case R.id.btnMenuTraffic_mudslide:
                actionBarTitle.setText(getString(R.string.txt_menu_mudslide_item));
                break;
            case R.id.btnMenuTraffic_flooding:
                actionBarTitle.setText(getString(R.string.txt_menu_flooding_item));
                break;
            case R.id.btnMenuTraffic_fire:
                actionBarTitle.setText(getString(R.string.txt_menu_fire_item));
                break;
            case R.id.btnMenuTraffic_other:
                actionBarTitle.setText(getString(R.string.txt_menu_traffic_group));
                break;
            case R.id.btnMenuDate_today:
                actionBarTitle.setText(getString(R.string.txt_menu_today_item));
                break;
            case R.id.btnMenuDate_tomorrow:
                actionBarTitle.setText(getString(R.string.txt_menu_tomorrow_item));
                break;
            case R.id.btnMenuDate_weekend:
                actionBarTitle.setText(getString(R.string.txt_menu_weekend_item));
                break;
            default:
                actionBarTitle.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/solgas.ttf"));
                actionBarTitle.setText(getString(R.string.app_name));
                break;
        }
        slidingMenu.showContent(true);
    }

    private void expand(View v) {
        //set Visible
        v.setVisibility(View.VISIBLE);

        if (Support.isAPIHigherHoneycomb()) {
            final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            v.measure(widthSpec, heightSpec);

            ValueAnimator mAnimator = slideAnimator(v, 0, v.getMeasuredHeight());
            mAnimator.start();
        }
    }

    private void collapse(final View v) {
        if (Support.isAPIHigherHoneycomb()) {
            int finalHeight = v.getHeight();
            ValueAnimator mAnimator = slideAnimator(v, finalHeight, 0);
            mAnimator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animator) {

                }

                @Override
                public void onAnimationEnd(Animator animator) {
                    //Height=0, but it set visibility to GONE
                    v.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationCancel(Animator animator) {

                }

                @Override
                public void onAnimationRepeat(Animator animator) {

                }
            });
            mAnimator.start();
        } else v.setVisibility(View.GONE);
    }

    private ValueAnimator slideAnimator(final View v, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                //Update Height
                int value = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
                if (layoutParams != null)
                    layoutParams.height = value;
                v.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }

    public MainMenuFragment newInstance(SlidingMenu slidingMenu, TextView actionBarTitle) {
        this.slidingMenu = slidingMenu;
        this.actionBarTitle = actionBarTitle;
        return this;
    }
}

